#include "table.h"

Table::Table(std::string &newName){
	name = newName;
}
std::string Table::getName(){
	return name;
}
void Table::addColumn(Column *col){
	cols.push_back(col);
}

void Table::addRow(Row *row){
	rows.push_back(row);
}

void Table::printAllRows(){
	for (std::list<Row*>::iterator it = rows.begin(); it != rows.end(); it++){
			Row *p = *it;
		    std::cout << "\n ----------------------- "  << std::endl;
		    p->printMapData();
	}
}

void Table::printCols(){
	for (std::list<Column*>::iterator it = cols.begin(); it != cols.end(); it++){
		Column *p = *it;
	    std::cout << "\n col: " << p->getName() << std::endl;
	}
}

std::list<Column*>& Table::getAllCols(){
	return cols;
}

Column* Table::getColumnByName(const std::string &colName){
	for (std::list<Column*>::iterator it = cols.begin(); it != cols.end(); it++){
		Column *p = *it;
		if( p->getName() == colName ){
//			std::cout << "\n found Column: " << p->getName() << std::endl;
			return p;
		}
	}
	return NULL;
}

bool Table::deleteRow(const std::string &colName, const std::string &value,  char &con){

	for (std::list<Row*>::iterator it = rows.begin(); it != rows.end(); it++){
		Row *p = *it;
		std::cout << "\n ----------------------- "  << std::endl;
		if( p->checkData(colName,value, con)){
			rows.erase(it);
			std::cout << "\n Deleted row .	" <<   std::endl;
			return true;
		}
	}
	std::cout << "\n Could not find row.	" << std::endl;
	return false;
}

std::list<Row*> Table::listRows(const std::string &colName, const std::string &value,  char &con){
	std::list<Row*> list;
	for (std::list<Row*>::iterator it = rows.begin(); it != rows.end(); it++){
		Row *p = *it;
		if( p->checkData(colName,value, con)){
			list.push_back(p);
		}
	}
	return list;
}

void Table::printRows(std::list<Row*> &rows,std::vector<std::string> &cols){
	std::string colNames = "\n ";
	for (  int i=0; i < (int)cols.size(); i++){
			colNames += cols[i] +"          ";
	}
	std::cout << colNames;
	std::string rowValue="\n";
	std::string tmpValue;
	for (std::list<Row*>::iterator itRow = rows.begin(); itRow != rows.end(); itRow++){
			rowValue = "\n";
			Row *r = *itRow;
			for (int i=0; i<(int)cols.size(); i++){
					rowValue += r->getData(cols[i]) + "   ";
			}
			std::cout << rowValue;
	}
}

std::list<Row*> Table::getAllRows(){
	return rows;
}

void Table::printAllRowsByColumn(std::vector<std::string> &cols){
	printRows(rows,cols);
}
